package com.yc.framework.config;

import com.yc.framework.interceptor.IpBanInterceptor;
import com.yc.framework.interceptor.IpLimitInterceptor;
import com.yc.framework.interceptor.TokenValidateInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private IpLimitInterceptor ipLimitInterceptor;

    @Autowired
    private IpBanInterceptor ipBanInterceptor;

    @Autowired
    private TokenValidateInterceptor tokenValidateInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 先添加IP禁止拦截器，优先检查IP是否被禁止
        registry.addInterceptor(ipBanInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/static/**", "/error", "/favicon.ico");

        // 添加Token验证拦截器，专门处理token验证接口的频率限制
        registry.addInterceptor(tokenValidateInterceptor)
                .addPathPatterns("/token/validate")
                .order(2);

        // 再添加IP限制拦截器，处理常规的访问频率限制
        registry.addInterceptor(ipLimitInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/static/**", "/error", "/favicon.ico", "/token/validate") // 排除token验证接口，由专门的拦截器处理
                .order(3);
    }
}